<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" showFooter :title="getTitle" width="80%" @ok="handleSubmit">

    <BasicForm @register="registerForm">

    </BasicForm>

  </BasicDrawer>
</template>
<script lang="ts" setup>
import { ref, computed, unref, h, toRaw, reactive } from 'vue';
import { Button,Empty } from 'ant-design-vue';
import { BasicForm, useForm } from '@/components/Form';
import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
import { useDrawer } from '@/components/Drawer';



import {
  addformSchema,
} from './role.data';

import { BasicModal, useModalInner } from '@/components/Modal';
import { Form, FormItem, Input, InputNumber, DatePicker, Select, Switch } from 'ant-design-vue';
import ChooseQuestionDrawer from './ChoosePaperQuestionDrawer.vue';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { addPublishPaper } from '@/api/heng/ex';
import { useMessage } from "@/hooks/web/useMessage";
import util from '@/utils/util';
const choose = useDrawer();

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const { createMessage } = useMessage();


const id = ref("")

const [registerForm, { resetFields, setFieldsValue, updateSchema, validate }] = useForm({
  labelWidth: 100,
  schemas: addformSchema,
  showActionButtonGroup: false,
  baseColProps: { lg: 12, md: 24 },
});


const dataList = ref({});
const groupList=ref([]);//{categoryName:"测试",type:1}


const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  //打开弹层后的回调
  // resetFields();
  setDrawerProps({ confirmLoading: false });
  resetFields();

  // isUpdate.value = !!data.isUpdate;
  // id.value = "";
  // groupList.value=[];
  // if (!isUpdate.value) {
  //   resetFields();
  // } else {
  //
  //   id.value = data.record.paperId;
  //   paperDetailFn();
  // }


});

const getTitle = computed(() => ('试卷发布'));



async function handleSubmit() {
  try {

    const values = await validate();
    console.log("--values:values")
    addPublishPaper(values).then(res => {
      sunccFn(res)
    });


  } catch (e) {
    console.log(e)
  } finally {
    setDrawerProps({ confirmLoading: false });

  }
}

function sunccFn(result) {
  if (result && result.code == 200) {
    createMessage.success(result.message);
  } else {
    createMessage.error(result.message);
  }
  closeDrawer()
  emit('success');
}

function cancelFn() {
  //  tableRef.value.clearValidate();
}

function getHeadStyle(head){
  let style = {};
  if(head.width){
    style.width=head.width+'px';
  }else{
    style.flex=1;
  }
  return style;
}

function getActions(record,index) {
  let arr = [
    {
      // icon: 'clarity:note-edit-line',
      label: '编辑',
      onClick: handleEdit.bind(null, record,index),
    },
    {
      // icon: 'ant-design:delete-outlined',
      label: '删除',
      color: 'error',
      popConfirm: {
        title: '是否删除？',
        placement: 'left',
        confirm: handleClose.bind(null, record,index),
      },
    }
  ];


  return arr;
}


function handleClose(record: Recordable,index) {
  groupList.value = groupList.value.slice(index,1);
  // tableRef.value.remove(record);

}

function addFn() {
  choose[1].openDrawer(true, {
    isUpdate: false,
  })
}

function handleSuccess(item) {

  if(item.isUpdate) {
    groupList.value[item.index]=item;
  }else{
    groupList.value.push(item);
  }
}

function handleEdit(record: Recordable,index) {
  record.isUpdate=true
  record.index = index;
  choose[1].openDrawer(true, record)
}


</script>
<style lang="less">
.item-row {
  margin: 0 auto;

  //padding-left: 5%;
  .ant-form-item-label {
    flex: 0 0 28.5%;
    max-width: 28.5%;
  }
}

.lqfp-8 {
  //padding-left: 9% !important;

}

.foot_item {
  display: flex;
  padding-top: 4px;
  padding-bottom: 2px;
  border-right: 1px solid #e8eaec;
  border-bottom: 1px solid #e8eaec;
  border-left: 1px solid #e8eaec;
  color: #606266;
}

.foot-c {
  justify-content: center;
  width: 100%;
}
.foot_item_row{
  width: 100%;
  text-align: center;
}
</style>
